﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace debioscoopincdrie.Managers
{
    class MovieManager
    {
        #region Fields
        private static MovieManager INSTANCE;
        #endregion

        #region Constructors
        private MovieManager()
        {
        }
        #endregion

        #region Static functions
        public static MovieManager GetManager()
        {
            if (INSTANCE == null)
            {
                INSTANCE = new MovieManager();
            }

            return INSTANCE;
        }
        #endregion

        #region Functions
        public List<film> GetMovies()
        {
            return DatabaseManager.GetManager().GetEntities().film.ToList();
        }

        public List<film> GetMoviesToday()
        {
            DateTime today = DateTime.Today;
            List<film> movies = new List<film>();

            foreach (planning planning in PlanningManager.GetManager().GetPlannings(today))
            {

                if (!movies.Contains(planning.film))
                {
                    movies.Add(planning.film);
                }
            }

            return movies;
        }

        public film GetMovie(String name)
        {
            film ret = null;

            foreach (film movie in GetMovies())
            {
                if (movie.naam.Equals(name))
                {
                    ret = movie;
                    break;
                }
            }

            return ret;
        }

        public int GetAvaibleSeats(String naam, DateTime date, TimeSpan time)
        {
            film movie = GetMovie( naam );
            planning planning = PlanningManager.GetManager().GetPlanning(movie.film_id, date, time);

            int avaibleSeats = 0;
            int seats = planning.zaal.zitting;
            int takenSeats = planning.kaartje.Count();
            avaibleSeats = seats - takenSeats;

            return avaibleSeats;
        }

        public int[] GetTakenSeats(String naam, DateTime date, TimeSpan time)
        {
            film movie = GetMovie(naam);
            planning planning = PlanningManager.GetManager().GetPlanning(movie.film_id, date, time);
            int[] takenSeats = new int[planning.kaartje.Count()];

            int current = 0;
            foreach (kaartje ticket in planning.kaartje)
            {
                if (ticket.zittingnummer != null)
                {
                    takenSeats[current] = (int)ticket.zittingnummer;
                }
                current++;
            }

            return takenSeats;
        }
        #endregion
    }
}
